var batchinv_caption = '清單細項:';
var batchinv_murl = '/store/rpc?q=batchinv';
var batchinv_durl = '/store/rpc?q=productinv&bid=';

jQuery("#dbm_batch_inv_mas").jqGrid({
  url:batchinv_murl,
	height: 130,
	datatype: "json",
  colNames:['編號','分類', '金額', '建立者', '建立時間','註記'],
  colModel:[
    {name:'id',index:'__key__', width:55,editable:false,editoptions:{readonly:true,size:10}},
    {name:'category',index:'category', width:90,editable:true,editoptions:{size:25}},
    {name:'total_value',index:'total_value', width:80,editable:true, align:"right",editoptions:{size:25}},
    {name:'create_user',index:'create_user', width:90,editable:false,editoptions:{size:25}},
    {name:'create_date',index:'create_date', width:90,editable:false,editoptions:{size:25}},
    {name:'note',index:'note', width:100, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"20"}}
  ],
  rowNum:5,
  rowList:[5,10,20],
  imgpath: gridimgpath,
  pager: jQuery('#dbm_batch_inv_mas_page'),
  viewrecords: true,
  sortorder: "asc",
  jsonReader: {
    repeatitems : false,
    id: "0"
  },
  multiselect: false,
  caption:"批次清單",
  editurl:"/store/batchinv",
  onSelectRow: function(ids) {
    if(ids == null) {
      ids=0;
      if(jQuery("#dbm_batch_inv_slav").getGridParam('records') >0 )
      {
        jQuery("#dbm_batch_inv_slav").setGridParam({url:batchinv_durl+ids,page:1})
        .setCaption(batchinv_caption+ids)
        .trigger('reloadGrid');
      }
    } else {
      row = jQuery("#dbm_batch_inv_mas").getRowData(ids);
      jQuery("#dbm_batch_inv_slav").setGridParam({url:batchinv_durl+ids,page:1})
      .setCaption('批次編號"'+ row.id+ '" 的' + batchinv_caption)
      .trigger('reloadGrid');			
    }
  }
}).navGrid('#dbm_batch_inv_mas_page',
  {add:true,edit:true,del:false, search:false},//bar option
  {height:280,reloadAfterSubmit:false}, // edit options
  {height:280,reloadAfterSubmit:false// add options
    , afterSubmit : function(response, postdata) {
      eval('res = ' + response.responseText);
      //jQuery('#dbm_batch_inv_slav').addRowData(res.id, res);
      return [true, "OK", res.id];
    }
  }, // add options
  {reloadAfterSubmit:false}, // del options
  {} // search options
  );
jQuery("#dbm_batch_inv_slav").jqGrid({
	height: 200,
  url: batchinv_durl+'1',
	datatype: "json",
  colNames:['ID','產品ID','分類','產品名','數量','金額'],
  colModel:[
    {name:'id',index:'__key__', width:55},
    {name:'pid',index:'product', width:90,editable:true,editoptions:{size:15}},
    {name:'category',index:'category', width:90,editable:false,editoptions:{size:25}},
    {name:'product',index:'product', width:90,editable:false,editoptions:{size:25}},
    {name:'quantity',index:'quantity', width:80,editable:true, align:"right",editoptions:{size:25}},
    {name:'value',index:'value', width:80,editable:true, align:"right",editoptions:{size:25}},
  ],

  rowNum:10,
  rowList:[10,20,30],
  imgpath: gridimgpath,
  pager: jQuery('#dbm_batch_inv_slav_page'),
  viewrecords: true,
  sortorder: "asc",
  multiselect: false,
  caption:batchinv_caption,
  editurl:"/store/productinv",
  jsonReader: {
    repeatitems : false,
    id: "0"
  }
}).navGrid('#dbm_batch_inv_slav_page',
  {add:true,edit:true,del:true, search:false}, //bar option
  {height:280,reloadAfterSubmit:false}, // edit options
  {height:280,reloadAfterSubmit:false// add options
    , beforeSubmit : function(postdata, formid) {
      bid = jQuery("#dbm_batch_inv_mas").getGridParam('selrow');
      postdata.bid = bid;
      row = jQuery('#dbm_batch_inv_mas').getRowData(bid);
      postdata.category = row.category;
      return [true, "OK", ""];
    }
    , afterSubmit : function(response, postdata) {
      eval('res = ' + response.responseText);
      //jQuery('#dbm_batch_inv_slav').addRowData(res.id, res);
      jQuery('#dbm_batch_inv_slav').setRowData(res.id, res)
      return [true, "OK", res.id];
    }
  }, // add options
  {reloadAfterSubmit:false}, // del options
  {} // search options
);
